package com.itheima.webmagic.demo;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.FilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

public class JobProcessor implements PageProcessor {

    public void process(Page page) {
        //CSS
        page.putField("css1", page.getHtml().css("div.mt>h3").all());
        page.putField("css2", page.getHtml().css("dl.self-service-list>dd a").all());
        //XPath
        page.putField("xpath", page.getHtml().xpath("//dl[@class=self-service-list]/dd/h3/a/text()").all());

        //正则表达式
        page.putField("zhengze", page.getHtml().css("dl.self-service-list>dd a").regex(".*订单.*").all());
    }

    private Site site = Site.me().setCharset("UTF-8")//编码
            .setSleepTime(1)//抓取间隔时间
            .setTimeOut(1000*10)//超时时间
            .setRetrySleepTime(3000)//重试时间
            .setRetryTimes(3);//重试次数
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new JobProcessor())
                //初始访问url地址
                .addUrl("https://help.jd.com/index.html")
                .addPipeline(new FilePipeline("D:\\IdeaProject\\localfile\\webmagic_result\\"))
                .thread(5)
                .run();
    }
}